package com.market.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.market.dto.OrderDetailDto;
import com.market.entity.Goods;
import com.market.entity.Order;
import com.market.entity.OrderDetail;
import com.market.service.GoodsService;
import com.market.service.OrderDetailService;
import com.market.vo.R;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class OrderDetailController {

    @Resource
    public OrderDetailService orderDetailService;
    @Resource
    public GoodsService goodsService;


    //新增某个订单的详情
    @PostMapping("/addOrderDetail")
    public R<OrderDetail>addOrderDetail(@RequestBody OrderDetail orderDetail){
        boolean b = orderDetailService.addOrderDetail(orderDetail);
        if (b){
            return new R<>(200,"订单新增成功!",orderDetail);
        }else {
            return new R<>(300,"订单查询失败!");
        }
    }
    //查询某个商品当日购买量
    @GetMapping("/GoodsAllBuyAmount")
    public R<OrderDetail>GoodsAllBuyAmount(int goodsId,String payTime){
        double v = orderDetailService.GoodsAllBuyAmount(goodsId, payTime);
        String goodsName = goodsService.queryGoodsByGoodsId(goodsId).getGoodsName();
        return new R<>(200,"查询成功!当日"+goodsName+"累计下单"+v+"份");
    }

    //通过id查询某个商品
    @GetMapping("/queryOrderDetail")
    public R<OrderDetail>queryOrderDetail(int id){
        OrderDetail orderDetail = orderDetailService.queryOrderDetail(id);
        if (orderDetail!=null){
            return new R<>(200,"订单查询成功!",orderDetail);
        }else {
            return new R<>(300,"订单查询失败!");
        }
    }

    //通过订单id查询订单详情
    @GetMapping("queryOrderDetailByOrderId")
    public R<Order>queryOrderDetailByOrderId(int orderId,int currentPage,int pageSize){
        PageHelper.startPage(currentPage,pageSize);
        List<OrderDetail> orderDetails = orderDetailService.queryOrderDetailByOrderId(orderId);
        if (orderDetails!=null){
            return new R(200,"查询订单成功",orderDetails);
        }else {
            return new R<>(300,"查无订单!");
        }
    }

    //通过订单id查询订单详情
    @GetMapping("queryOrderDetailsByOrderIdDto")
    public R<Order>queryOrderDetailsByOrderIdDto(int orderId,int currentPage,int pageSize){
        PageHelper.startPage(currentPage,pageSize);
        List<OrderDetailDto> orderDetails = orderDetailService.queryOrderDetailsByOrderIdDto(orderId);
        if (orderDetails!=null){
            return new R(200,"查询订单成功",new PageInfo<>(orderDetails));
        }else {
            return new R<>(300,"查无订单!");
        }
    }

}
